package com.webcamtracker.configuration;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.prefs.BackingStoreException;
import java.util.prefs.NodeChangeListener;
import java.util.prefs.PreferenceChangeListener;
import java.util.prefs.Preferences;


public class CachedPreferences extends Preferences {
    private Preferences preferences;
    private Map<String, String> cached;
    private Map<String, CachedPreferences> childs;

    public CachedPreferences(Preferences preferences) {
        this.preferences = preferences;
        childs = new HashMap<String, CachedPreferences>();
        cached = new HashMap<String, String>();
    }

    public void put(String key, String value) {
        cached.put(key, value);
        preferences.put(key, value);
    }

    public String get(String key, String def) {
        if (cached.get(key) != null) {
            return cached.get(key);
        } else {
            String value = preferences.get(key, def);
            cached.put(key, value);
            return value;
        }
    }

    public void remove(String key) {
        cached.remove(key);
        preferences.remove(key);
    }

    public void clear() throws BackingStoreException {
        cached.clear();
        preferences.clear();
    }

    public int getInt(String key, int def) {
        int result = def;
        try {
            String value = get(key, null);
            if (value != null)
                result = Integer.parseInt(value);
        } catch (NumberFormatException e) {
            // Ignoring exception causes specified default to be returned
        }

        return result;
    }


    public void putLong(String key, long value) {
        put(key, Long.toString(value));
    }


    public long getLong(String key, long def) {
        long result = def;
        try {
            String value = get(key, null);
            if (value != null)
                result = Long.parseLong(value);
        } catch (NumberFormatException e) {
            // Ignoring exception causes specified default to be returned
        }

        return result;
    }

    public void putBoolean(String key, boolean value) {
        put(key, String.valueOf(value));
    }

    public boolean getBoolean(String key, boolean def) {
        boolean result = def;
        String value = get(key, null);
        if (value != null) {
            if (value.equalsIgnoreCase("true"))
                result = true;
            else if (value.equalsIgnoreCase("false"))
                result = false;
        }

        return result;
    }

    public void putFloat(String key, float value) {
        put(key, Float.toString(value));
    }

    public float getFloat(String key, float def) {
        float result = def;
        try {
            String value = get(key, null);
            if (value != null)
                result = Float.parseFloat(value);
        } catch (NumberFormatException e) {
            // Ignoring exception causes specified default to be returned
        }

        return result;
    }

    public void putDouble(String key, double value) {
        put(key, Double.toString(value));
    }

    public double getDouble(String key, double def) {
        double result = def;
        try {
            String value = get(key, null);
            if (value != null)
                result = Double.parseDouble(value);
        } catch (NumberFormatException e) {
            // Ignoring exception causes specified default to be returned
        }

        return result;
    }

    public void putByteArray(String key, byte[] value) {
        preferences.putByteArray(key, value);
    }

    public byte[] getByteArray(String key, byte[] def) {
        return preferences.getByteArray(key, def);
    }

    public void putInt(String key, int value) {
        put(key, Integer.toString(value));
    }

    public String[] keys() throws BackingStoreException {
        return preferences.keys();
    }

    public String[] childrenNames() throws BackingStoreException {
        return preferences.childrenNames();
    }

    public Preferences parent() {
        return preferences.parent();
    }

    public Preferences node(String pathName) {
        if (childs.get(pathName) != null) {

            return childs.get(pathName);
        } else {

            CachedPreferences result = new CachedPreferences(preferences.node(pathName));
            childs.put(pathName, result);
            return result;
        }
    }

    public boolean nodeExists(String pathName) throws BackingStoreException {
        return preferences.nodeExists(pathName);
    }

    public void removeNode() throws BackingStoreException {
        preferences.removeNode();
    }

    public String name() {
        return preferences.name();
    }

    public String absolutePath() {
        return preferences.absolutePath();
    }

    public boolean isUserNode() {
        return preferences.isUserNode();
    }

    public String toString() {
        return preferences.toString();
    }

    public void flush() throws BackingStoreException {
        preferences.flush();
    }

    public void sync() throws BackingStoreException {
        cached.clear();
        preferences.sync();
    }

    public void addPreferenceChangeListener(PreferenceChangeListener pcl) {
        preferences.addPreferenceChangeListener(pcl);
    }

    public void removePreferenceChangeListener(PreferenceChangeListener pcl) {
        preferences.removePreferenceChangeListener(pcl);
    }

    public void addNodeChangeListener(NodeChangeListener ncl) {
        preferences.addNodeChangeListener(ncl);
    }

    public void removeNodeChangeListener(NodeChangeListener ncl) {
        preferences.removeNodeChangeListener(ncl);
    }

    public void exportNode(OutputStream os) throws IOException, BackingStoreException {
        preferences.exportNode(os);
    }

    public void exportSubtree(OutputStream os) throws IOException, BackingStoreException {
        preferences.exportSubtree(os);
    }


}
